package com.lidroid.xutils.sample.menuapp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.sample.menuapp.adaptor.MenusAdaptor;
import com.lidroid.xutils.sample.menuapp.enty.Dish;
import com.lidroid.xutils.sample.menuapp.enty.Step;

public class ClientsAPI {
	private static String startId;

	public ClientsAPI() {
		// TODO Auto-generated constructor stub
	}

	public static JSONObject ParseJson(final String path, final String encode) {
		// TODO Auto-generated method stub
		HttpClient httpClient = new DefaultHttpClient();
		HttpParams httpParams = httpClient.getParams();
		// HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
		// HttpConnectionParams.setSoTimeout(httpParams, 10000);
		HttpPost httpPost = new HttpPost(path);
		try {
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String result = EntityUtils.toString(httpResponse.getEntity(),
						encode);
				JSONObject jsonObject = new JSONObject(result);
				return jsonObject;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			return null;

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			if (httpClient != null)
				httpClient.getConnectionManager().shutdown();
		}
		return null;

	}


	  
	public static ArrayList<Dish> getWangQuData(String Url) {
		ArrayList<Dish> list = new ArrayList<Dish>();
		JSONObject jsonData = ParseJson(Url, "utf-8");
		System.out.println(jsonData);

		if (jsonData == null) {
			return null;
		} else {
			JSONArray Data;		 
			try {
				Data = jsonData.getJSONObject("result").getJSONArray("data");
				for (int i = 0; i < Data.length(); i++) {
					Dish dish = new Dish();
					JSONObject dishObject = Data.getJSONObject(i);
					dish.setId(dishObject.getString("id"));
					dish.setName(dishObject.getString("title"));
					dish.setImtro(dishObject.getString("imtro"));
					dish.setTags(dishObject.getString("tags"));
					dish.setIngredients(dishObject.getString("ingredients"));
					dish.setBurden(dishObject.getString("burden"));
					JSONArray albumsArray = dishObject.getJSONArray("albums");
					// String[] albumsStrArr= new String[albumsArray.length()];
					String albumsStr = "";
					for (int j = 0; j < albumsArray.length(); j++) {
						albumsStr = albumsArray.getString(j);
						dish.setAlbums(albumsStr);
					}
					JSONArray stepsJsonArray = dishObject.getJSONArray("steps");
                  Step[] stepsArr=new Step[stepsJsonArray.length()];
					for (int j = 0; j < stepsJsonArray.length(); j++) {
						Step step = new Step();
						JSONObject stepoObject = stepsJsonArray.getJSONObject(j);
						step.setImg(stepoObject.getString("img"));
						step.setStep(stepoObject.getString("step"));
						 stepsArr[j]=step;

					}
					dish.setSteps(stepsArr);
					list.add(dish);

				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("------->" + list.size());
			return list;
		}

	}


}
